<?php
namespace app\api\controller\cart;

use think\Controller;

class Cart extends Controller{

    public function __construct()
    {
        parent::__construct();

        $this->TypeModel = model('Product.Type');
        $this->ProductModel = model('Product.Product');
        $this->UserModel = model('User.User');
        $this->VipModel = model('Product.Vip');
        $this->CartModel = model('Cart');

    }

    // 添加购物车
    public function add()
    {
        if($this->request->isPost())
        {
            $proid = $this->request->param('proid',0);
            $userid = $this->request->param('userid', 0);

            $userinfo = $this->UserModel->find($userid);
            
            //当没有找到用户的情况下
            if(!$userinfo){
                $this->error('用户不存在，请重新输入');
                exit;
            }

            $productinfo = $this->ProductModel->find($proid);

            //当没有找到用户的情况下
            if(!$productinfo){
                $this->error('商品不存在，请重新选择');
                exit;
            }

            // 判断库存
            if(!$productinfo['stock']){

                $this->error('商品库存不足');
                exit;
            }

            $vip = isset($userinfo) ? $userinfo['vip'] : 0;

            // 查询产品vip的价格
            if($vip > 0 ){
                $level = $this->VipModel->where(['proid'=> $proid, 'level' => $vip])->value('price');

                $price = $level ? $level : $productinfo['price'];

            } else{
                $price = $productinfo['price'];
            }
            
      

            //先判断该产品是否有添加到购物车
            $cart = $this->CartModel->where(['userid' => $userid, 'proid' => $proid])->find();

            if($cart)
            {
                $nums = $cart['nums'] + 1;
                //总价  bcmul:精确乘法
                $total = bcmul($nums, $price);

                $data = [
                    'id' => $cart['id'],
                    'userid' => $userid,
                    'proid' => $proid,
                    'price' => $price,
                    'nums' => $nums,
                    'total' => $total
                ];

                $result  =  $this->CartModel->isUpdate()->save($data);

                if($result === FALSE)
                {
                    $this->error($this->CartModel->getError());
                    exit;
                }else
                {
                    $this->success('添加购物车成功,是否跳转到购物车立即下单', '/product/cart/index');
                    exit;
                }

            } else{
                $data = [
                    'userid' => $userid,
                    'proid' => $proid,
                    'price' => $price,
                    'nums' => 1,
                    'total' => $price
                ];

                $result =  $this->CartModel->save($data);

                if($result === FALSE)
                {
                    $this->error($this->CartModel->getError());
                    exit;
                }else
                {
                    $this->success('添加购物车成功,是否跳转到购物车立即下单', '/product/cart/index');
                    exit;
                }

            }
        }
    }

    // 购物车列表
    public function index()
    {
        if($this->request->isPost())
        {
            $cartids = $this->request->param('cartids', '');
            $userid = $this->request->param('userid', 0);
            
            $userinfo = $this->UserModel->find($userid);
            
            //当没有找到用户的情况下
            if(!$userinfo){
                $this->error('用户不存在，请重新输入');
                exit;
            }

            $where = [];

            if($userid)
            {
                $where['userid'] = $userid;
            }

            if($cartids){
                $where['cart.id'] = ['in', $cartids];
            }

            $result = $this->CartModel->with('product')->where($where)->select();

            if($result === FALSE)
            {
                $this->error($this->CartModel->getError());
                exit;
            }else
            {
                $this->success('查询成功', null, $result);
                exit;
            }
        }
    }

    // 编辑购物车
    public function edit()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $id = $this->request->param('id', 0);
            $nums = $this->request->param('nums', 0);

            
            $userinfo = $this->UserModel->find($userid);
            
            //当没有找到用户的情况下
            if(!$userinfo){
                $this->error('用户不存在，请重新输入');
                exit;
            }

            $cart = $this->CartModel->find($id);

            //当没有找到用户的情况下
            if(!$cart){
                $this->error('购物车数据不存在');
                exit;
            }

            $proid = $cart['proid'] ? $cart['proid'] : 0;

            $product = $this->ProductModel->find($proid);

            if(!$product)
            {
                $this->error('商品不存在，请重新选择', '/product/product/index');
                exit;
            }

            // 判断商品库存
            if(!$product['stock']){

                $this->error('商品库存不足');
                exit;
            }

            $total = bcmul($nums, $cart['price']);

            $data = [
                'id' => $id,
                'nums' =>$nums,
                'total' => $total
            ];

            $result  =  $this->CartModel->isUpdate()->save($data);

            if($result === FALSE)
            {
                $this->error($this->CartModel->getError());
                exit;
            }else
            {
                $this->success('修改购物车成功');
                exit;
            }

        }
    }

    // 删除购物车
    public function del()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $id = $this->request->param('id', 0); 
             
            $userinfo = $this->UserModel->find($userid);
            
            //当没有找到用户的情况下
            if(!$userinfo){
                $this->error('用户不存在，请重新输入');
                exit;
            }

            $cart = $this->CartModel->find($id);

            //当没有找到用户的情况下
            if(!$cart){
                $this->error('购物车数据不存在');
                exit;
            }

            $result = $this->CartModel->destroy($id);

            if($result === FALSE)
            {
                $this->error($this->CartModel->getError());
                exit;
            }else
            {   
                $data = [
                    'id' => $cart['proid'],
                    'stock' => $cart['nums']
                ];
                
                $res  = $this->ProductModel->isUpdate()->save($data);

                $this->success('删除购物车成功');
                exit;
            }

        }
    }
}
?>